<template>
  <div class="menu-container">
    <!-- 顶部导航栏 -->
    <div class="header">
      <div class="logo">
        <h2>MES 生产管理系统</h2>
      </div>
      <div class="user-info">
        <el-dropdown>
          <span class="user-dropdown">
            <el-icon><User /></el-icon>
            管理员
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>个人信息</el-dropdown-item>
              <el-dropdown-item>修改密码</el-dropdown-item>
              <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>

    <!-- 主体内容区 -->
    <div class="main-content">
      <!-- 侧边栏菜单 -->
      <div class="sidebar">
        <el-menu
          :router="true"
          :default-active="activeMenu"
          class="menu-vertical"
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF">
          
          <template v-for="item in menuList" :key="item.menuId">
            <!-- 有子菜单的情况 -->
            <el-sub-menu 
              v-if="item.children && item.children.length > 0"
              :index="item.menuId.toString()">
              <template #title>
                <i v-if="item.icon" :class="item.icon"></i>
                <span>{{ item.menuName }}</span>
              </template>
              
              <!-- 递归渲染子菜单 -->
              <template v-for="child in filterNonButtonItems(item.children)" :key="child.menuId">
                <el-menu-item :index="child.url || child.menuId.toString()">
                  <i v-if="child.icon" :class="child.icon"></i>
                  <span>{{ child.menuName }}</span>
                </el-menu-item>
              </template>
            </el-sub-menu>
            
            <!-- 没有子菜单的情况 -->
            <el-menu-item
              v-else
              :index="item.url || item.menuId.toString()">
              <i v-if="item.icon" :class="item.icon"></i>
              <span>{{ item.menuName }}</span>
            </el-menu-item>
          </template>
        </el-menu>
      </div>
      
      <!-- 内容区域 -->
      <div class="content">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script>
import { User } from '@element-plus/icons-vue'

export default {
  name: 'AppMenu',
  components: {
    User
  },
  data() {
    return {
      menuList: [], // 菜单数据
      activeMenu: '', // 当前激活的菜单
      // 图标映射表
      iconMap: {
        'fa fa-gear': 'Setting',
        'fa fa-user': 'User',
        'fa fa-location': 'Location',
        'fa fa-file': 'Document',
        'fa fa-menu': 'Menu'
        // 可以根据需要添加更多图标映射
      }
    }
  },
  created() {
    this.getMenuList()
    this.activeMenu = this.$route.path
  },
  methods: {
    async getMenuList() {
      try {
        const res = await this.$api.getMenuList()
        if (res.data.code === 200) {
          this.menuList = res.data.data
          console.log(this.menuList)
        }
      } catch (error) {
        console.error('获取菜单失败:', error)
      }
    },
    handleLogout() {
      this.$router.push('/UserLogin')
    },
    // 修改过滤方法
    filterNonButtonItems(items) {
      if (!items) return []
      return items.filter(item => {
        // 检查是否是按钮类型的权限
        const isButton = item.perms && (
          item.perms.includes(':add') ||
          item.perms.includes(':edit') ||
          item.perms.includes(':delete') ||
          item.perms.includes(':export') ||
          item.perms.includes(':import')
        )
        return !isButton // 返回非按钮类型的菜单
      })
    },
    // 判断是否是按钮
    isButton(item) {
      return item.perms && (
        item.perms.includes(':add') ||
        item.perms.includes(':edit') ||
        item.perms.includes(':delete') ||
        item.perms.includes(':export') ||
        item.perms.includes(':import')
      )
    },
    // 添加图标转换方法
    getIcon(icon) {
      return this.iconMap[icon] || icon
    }
  },
  watch: {
    $route: {
      handler(route) {
        this.activeMenu = route.path
      },
      immediate: true
    }
  }
}
</script>

<style scoped>
.menu-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  height: 60px;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
}

.logo h2 {
  margin: 0;
  color: #304156;
}

.user-info {
  cursor: pointer;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 8px;
}

.main-content {
  flex: 1;
  display: flex;
}

.sidebar {
  width: 200px;
  background-color: #304156;
}

.menu-vertical {
  border-right: none;
  height: 100%;
}

.content {
  flex: 1;
  padding: 20px;
}

:deep(.el-menu) {
  border-right: none;
}

:deep(.el-sub-menu .el-menu-item) {
  min-width: 200px;
}

/* 添加图标样式 */
i[class^="fa"] {
  margin-right: 10px;
  width: 24px;
  text-align: center;
  font-size: 18px;
  vertical-align: middle;
}
</style>

 